package com.sunelec.controller;

import com.sunelec.pojo.Power;
import com.sunelec.pojo.SunElecResult;
import com.sunelec.pojo.Users;
import com.sunelec.service.UserService;
import com.sunelec.utils.IDUtils;
import com.sunelec.utils.MethodUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: 郑明利
 * @Description:
 * @Date: Create in 10:35 2018/04/23
 * @Modified By:
 */
@Controller
public class UserRegisterController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/registerInfo", method = RequestMethod.POST)
    public String GetRegisterInfo(String username, String password, HttpSession session, Model model){
        if(userService.checkData(username,1).getStatus() == 400){
            model.addAttribute("msg","用户名已占用");
            return "register";
        }
        session.setAttribute("RegisterUsername",username);
        session.setAttribute("RegisterPassword",password);
        return "addStation";
    }

    @RequestMapping("/registerUserPower")
    public String UserRegister(String addType, String powerName,String powerType, String country,
                               String city,String local,String capacity,Integer powerAvgElec,
                               Integer powerConCost,Integer powerConSubsidy,String phone,String repayMethod,
                               String powerSn, HttpSession session, Model model,
                               HttpServletRequest request) throws Exception{
        if(addType==null){
            addType="register";
        }
        if(addType.equals("add")){
            Users users = (Users) session.getAttribute("users");
            String username = users.getUsersname();
            StringBuilder sb = new StringBuilder();
            //MethodUtil.getFilePath(request,powerPic,sb);
            Power power = new Power();
            power.setPowerId("PowerStation_" + IDUtils.getId());
            power.setPowerName(powerName);
            power.setPoweType(powerType);
            power.setPowerLocal(country+city+local);
            power.setInstallCapacity(capacity);
            power.setPowerAvgElec((float)powerAvgElec);
            power.setPowerConCost((float)powerConCost);
            power.setPowerConSubsidy((float)powerConSubsidy);
            power.setRepayMethod(repayMethod);
            power.setPowerSn(powerSn);
            power.setPowerPoster(sb.toString());
            power.setOwn(username);
            power.setRuningId(1);
            SunElecResult sunElecResult = userService.createPower(power,username);
            if (sunElecResult.getStatus() == 200){
                model.addAttribute("status",200);
                model.addAttribute("msg", "添加成功");
            }else {
                model.addAttribute("status",500);
                model.addAttribute("msg", "电站信息异常,请核实");
            }
            return "addStation";
        }
        //判断Session中是否存在有注册用户信息
        String username = null;
        String password = null;
        int user_state = 0;
        try {
            username = (String) session.getAttribute("RegisterUsername");
            password = (String) session.getAttribute("RegisterPassword");
            //将用户信息写入user表
            Users users = new Users();
            users.setUserId("user_"+IDUtils.getId());
            users.setUsersname(username);
            users.setPassword(password);
            users.setPhone(phone);
            users.setType(3);
            user_state = userService.createUser(users).getStatus();
        }catch (Exception e){
            model.addAttribute("msg", "用户信息异常,请核实");
        }
        if(user_state == 200){
            StringBuilder sb = new StringBuilder();
            //MethodUtil.getFilePath(request,files,sb);
            Power power = new Power();
            power.setPowerId("PowerStation_" + IDUtils.getId());
            power.setPowerName(powerName);
            power.setPoweType(powerType);
            power.setPowerLocal(country+city+local);
            power.setInstallCapacity(capacity);
            power.setPowerAvgElec((float)powerAvgElec);
            power.setPowerConCost((float)powerConCost);
            power.setPowerConSubsidy((float)powerConSubsidy);
            power.setRepayMethod(repayMethod);
            power.setPowerSn(powerSn);
            power.setPowerPoster(sb.toString());
            power.setOwn(username);
            power.setRuningId(1);
            //将信息写入Power表
            SunElecResult sunElecResult = userService.createPower(power,username);
            //将关联关系写入relationship表和relationship2表
            SunElecResult sunElecResult1 = userService.makeRelationship(power.getPowerId(),username);
            String[] powerSN = powerSn.split(",");
            int args = 0;
            for(int i=0;i<powerSN.length;i++){
                SunElecResult sunElecResult2 = userService.makeRelationship2(power.getPowerId(),powerSN[i]);
                if(sunElecResult2.getStatus() != 200){
                    args = 1;
                }
            }
            //SunElecResult
            if (sunElecResult.getStatus() == 200 && sunElecResult1.getStatus()==200 && args==0){
                return "home";
            }else {
                model.addAttribute("msg", "电站信息异常,请核实");
                return "addStation";
            }
        }else {
            model.addAttribute("msg","请您先完成注册");
            return "register";
        }
    }
}
